package com.iap.client.mediator
{
	import com.iap.admin.event.LoginEvent;
	import com.iap.admin.signal.LoginRequestSignal;
	import com.iap.admin.view.AdminView;
	import com.iap.app.AppGlobal;
	import com.iap.app.StatusCode;
	import com.iap.app.model.Status;
	import com.iap.app.util.GlideTransitionEffect;
	import com.iap.client.event.MainEvent;
	import com.iap.client.view.ClientView;
	import com.iap.client.view.LoginErrorView;
	import com.iap.client.view.MainMenuView;
	import com.iap.client.view.MainView;
	import com.iap.report.view.ReportView;
	import com.iap.common.model.User;
	
	import flash.events.Event;
	
	import mx.core.UIComponent;
	import mx.managers.CursorManager;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Group;
	
	public class MainMediator extends Mediator
	{
		[Inject]
		public var view:MainView;
		
		[Inject]
		public var request:LoginRequestSignal;

		private var loginError:LoginErrorView; 
		
		override public function onRegister():void
		{
			addContextListener(MainEvent.SELECT_ADMIN, initAdmin);
			addContextListener(MainEvent.SELECT_CLIENT, initClient);
			addContextListener(MainEvent.SELECT_HOME, initHome);
			addContextListener(MainEvent.SELECT_REPORT, initReport);
			addContextListener(LoginEvent.RESPONSE, loginResponse);
			dispatchLoginRequest();
		}
		
		private function dispatchLoginRequest():void
		{
			var login:User = new User();
			login.userName = AppGlobal.autoLoginUserName;
			login.userPass = AppGlobal.autoLoginUserPass;
			request.dispatch(login);
			AppGlobal.startWait();
		}
		
		private function loginResponse(evt:LoginEvent):void
		{
			AppGlobal.endWait();
			var status:Status = evt.response.status;
			if(status.code == StatusCode.OK)
			{
				initMainContent();
			} else 
			{
				openConnectPopup(status.message);
			}
		}
		
		private function initClient(evt:Event = null):void
		{
			var container:Group = view.mainGroup;
			var oldView:UIComponent = container.getElementAt(0) as UIComponent;
			var newView:UIComponent = new ClientView();
			container.addElement(newView);
			GlideTransitionEffect.start(newView, oldView);
		}

		private function initAdmin(evt:Event = null):void
		{
			var container:Group = view.mainGroup;
			var oldView:UIComponent = container.getElementAt(0) as UIComponent;
			var newView:UIComponent = new AdminView();
			container.addElement(newView);
			GlideTransitionEffect.start(newView, oldView);
		}
		
		private function initHome(evt:Event = null):void
		{
			var container:Group = view.mainGroup;
			var oldView:UIComponent = container.getElementAt(0) as UIComponent;
			var newView:UIComponent = new MainMenuView();
			container.addElement(newView);
			GlideTransitionEffect.start(newView, oldView);
		}
		
		private function initReport(evt:Event = null):void
		{
			var container:Group = view.mainGroup;
			var oldView:UIComponent = container.getElementAt(0) as UIComponent;
			var newView:UIComponent = new ReportView();
			container.addElement(newView);
			GlideTransitionEffect.start(newView, oldView);
		}
		
		private function loginErrorReconnectHandler(action:String):void
		{
			dispatchLoginRequest();
		}

		public function openConnectPopup(message:String):void
		{
			loginError = PopUpManager.createPopUp(view, LoginErrorView, false) as LoginErrorView;
			loginError.message = message;
			loginError.closeButton.visible = false;
			PopUpManager.centerPopUp(loginError);
			loginError.close.add(reconnectClickHandler);
			GlideTransitionEffect.rotate3DFront(loginError);
		}

		protected function reconnectClickHandler():void
		{
			PopUpManager.removePopUp(loginError);
			dispatchLoginRequest();
		}
		
		private function initMainContent():void
		{
			view.mainGroup.enabled = true;
			view.mainGroup.getChildAt(0).alpha = 1;
			GlideTransitionEffect.rotate3Dback(view.mainGroup);
		}
	}
}